package com.yunquan.ohana.ui.calendar

import androidx.databinding.ObservableField
import com.yunquan.ohana.R
import com.yunquan.ohana.base.ui.BaseViewModel
import com.yunquan.ohana.base.ui.ItemViewModel
import com.yunquan.ohana.entity.ScheduleModel
import com.yunquan.ohana.utils.DateUtils
import com.yunquan.ohana.utils.Utils

class EventItemViewModel<T : BaseViewModel>(viewModel: T) : ItemViewModel<T>(viewModel) {
    var data: ScheduleModel? = null
    val title = ObservableField<String>()
    val dateTime = ObservableField<String>()
    val rolePrefixes = ObservableField<String>()

    constructor(viewModel: T, data: ScheduleModel) : this(viewModel) {
        this.data = data
        val role = data.scheduleToRoleVos?.firstOrNull()
        val roleName = role?.roleName?.firstOrNull()?.toString() ?: ""
        val startTime = data.startTime?.toLong() ?: 0
        val endTime = data.endTime?.toLong() ?: 0
        val startStr = DateUtils.date2Str(startTime * 1000L, DateUtils.FORMAT.HH_MM_A)
        val endStr = DateUtils.date2Str(endTime * 1000L, DateUtils.FORMAT.HH_MM_A)
        val isAllDay = data.allDay == 1
        val time = if (isAllDay) Utils.instance.context.getString(R.string.all_day) else "$startStr-$endStr"

        this.title.set(data.title)
        this.dateTime.set(time)
        this.rolePrefixes.set(roleName)
    }
}